import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:ugrownews_flutter/main/ScreenMain.dart';
import 'package:ugrownews_flutter/main/ScreenNewsDetail.dart';

class RouterUtil {
  //暂时还没办法实现滑动返回，没有大规模使用
  static const String ROUTER_NEWSDETAIL = "newsDetail/";
  static const String ROUTER_MAIN = "main/";

  static BuildContext buildContext;
  static Router _router = Router();
  static bool _isInited = false;

  static Router getInstance() {
    if (!_isInited) {
      initRouter();
      _isInited = true;
    }
    return _router;
  }

  static void setContexdt(@required BuildContext context) {
    if (context != null && context is BuildContext) {
      buildContext = context;
    } else {
      throw new Exception("context must be BuildContext");
    }
  }

  static void navigateToForPush(String path) {
    if (buildContext != null) {
      _router.navigateTo(buildContext, path,
          transition: TransitionType.cupertino);
    }
  }

  static void initRouter() {
    _router.define(
      ROUTER_NEWSDETAIL,
      handler: new Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
          return new ScreenNewsDetail(id: params["id"][0]);
        },
      ),
    );

    _router.define(
      ROUTER_MAIN,
      handler: new Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
          return new ScreenMain();
        },
      ),
    );
  }
}
